package com.senac.service;

import com.senac.exception.DisciplinaException;
import com.senac.exception.DisciplinaException.DISC_ERROR;
import com.senac.model.Disciplina;
import com.senac.model.Professor;

public class DisciplinaValidate {

	public static void validateDisciplina(Disciplina disciplina) throws DisciplinaException {
		
		
		if(disciplina.getNome() == null || disciplina.getNome().equals("")) {
			DisciplinaException ex = new DisciplinaException(DISC_ERROR.DISC_NAME_EMPTY);
			throw ex;
		}
		
		if(disciplina.getNome().length() < 8 || disciplina.getNome().length() > 80) {
			DisciplinaException ex = new DisciplinaException(DISC_ERROR.DISC_NAME_INVALID_LENGTH);
			throw ex;
		}
		
		if(disciplina.getConteudo() == null || disciplina.getConteudo().equals("")) {
			DisciplinaException ex = new DisciplinaException(DISC_ERROR.DISC_CONT_EMPTY);
			throw ex;
		}
		
		if(disciplina.getConteudo().length() < 10 || disciplina.getConteudo().length() > 140) {
			DisciplinaException ex = new DisciplinaException(DISC_ERROR.DISC_CONT_INVALID_LENGTH);
			throw ex;
		}
		
		if(disciplina.getProfessor() != null && disciplina.getProfessor().getId() > 0){
			
			ProfessorServiceLocal professorService = new ProfessorService();
			Professor professor = professorService.getById(disciplina.getProfessor().getId());
			
			int cargaHoraria = new ProfessorService().getCargaHorariaTotal(disciplina.getProfessor().getId(), disciplina.getId());
			
			cargaHoraria += disciplina.getCargaHoraria();
			if(professor.getCargaHoraria() < cargaHoraria) {
				DisciplinaException ex = new DisciplinaException(DISC_ERROR.DISC_CARGA_HORARIA);
				throw ex;
			}
		}
	}

}
